/**
 * 试剂耗材详情
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/6
 */
var self = phone = id = uid = tid =  post_man = '';

var number = 0;

document.addEventListener('plusready' , ready ,  false);
 
function ready(){
	
	self = plus.webview.currentWebview();
	
	var memberInfo = plus.storage.getItem('memberInfo');
	
	memberInfo = $.parseJSON(memberInfo);
	
	uid = memberInfo.aid; 
	
	post_man = self.uid;
	
	if(post_man === memberInfo.aid){
		$('.deleteBtn').html('<span class="iconfont icon-shanchu mui-pull-right deletBtn"></span>');
		
		$('.deleteBtn').on('click' , function(){
			//删帖
	 		plus.nativeUI.showWaiting();
			$.ajax({  
 				type:"post", 
	 			url:apiRoot + '/Home/Article/deletePost/',
	 			data:{
	 				id : self.aid 
	 			}, 
	 			success : function(data){ 
	 				plus.nativeUI.closeWaiting();  
	 				if(data == 1){ 
	 					toast('删除成功');
		   				if(self.page){
		   					mui.fire(plus.webview.getWebviewById(self.page) , 'add_post' ,{
			 					id : self.aid
							});
		   				}
		   				setTimeout(function(){
		   					self.close();
		   				} , 1000);
	 				}
	 			},
	 			error : function(e){
	 				console.log(JSON.stringify(e))
	 				toast('连接超时,请检查网络');
	 				plus.nativeUI.closeWaiting();
	 			}
	 			
	 		});
		})
		
	}
	
	
	$('.title').text(self.title);
	$('.addtime').text(self.addtime);
	number = self.bounty;
	$('.bounty').text(number); 
	$('.nickname').text(self.nickname);
	$('.avatar').attr('src' , getImgUrl(self.avatar));
	 
//	var html = self.body.indexOf('src="data:image/jpeg;base64') >= 0 ? self.body : (self.body.indexOf('src="http') >= 0 ? self.body : self.body.replace(/src="/g , 'src="' + webRoot))
	
	$('.content').html(self.body);
	 
	id = self.aid; 
	
	tid = self.tid;
	
 	info();
 	
 	
 	//收藏
 	$('.collection').on('click' , function(){
 		plus.nativeUI.showWaiting('操作中...'); 
 		$.ajax({  
 			type:"post", 
 			url:apiRoot + '/Home/Article/collection/',
 			data:{
 				uid : uid , 
 				aid : id ,
 				tid : tid
 			},
 			success : function(data){ 
 				plus.nativeUI.closeWaiting();  
 				var type = '';
 				if(data == 0){ 
 					toast('已取消');
 					type = '取消收藏';
 					$('.collection_status').css('color' , 'aliceblue');
 				}else{
 					toast('已收藏');
   					$('.collection_status').css('color' , 'darkgoldenrod');
 				}
 				
   				if(self.page){
   					mui.fire(plus.webview.getWebviewById(self.page) , 'cancel' ,{
	 					id : id  ,
	 					type : type
					});
   				}
 			},
 			error : function(e){
 				console.log(JSON.stringify(e))
 				toast('连接超时,请检查网络');
 				plus.nativeUI.closeWaiting();
 			}
 			
 		});
 	})
 	//点击分享
	$('.share').each(function(index) { 
		var type = ex = content = ''  
		$(this).on('click', function() {
			if(index == 0) {type = 'weixin' ; ex = 'WXSceneSession'; content = '';} 
			if(index == 1) {type = 'qq' ; ex = ''; content = $('.content').text() ; } 
			if(index == 2) {type = 'weixin' ; ex = 'WXSceneTimeline';content = '';}
			shareAction(type, ex , id , '端砚生物' , '' , '' , uid , tid);
		})
	})
 	//回帖
 	$('.reply').on('click' , function(){
 		plus.nativeUI.showWaiting('跳转中...');
 		var bbs_inf_huifu = plus.webview.create('bbs-inf-huifu.html', 'bbs-inf-huifu', {}, {
			page : self.id,
			top_page : self.page,
			uid : memberInfo.aid, 
			aid : id,
			title : self.title,
			nickname : memberInfo.nickname,
			avatar : memberInfo.avatar
		}); 
		bbs_inf_huifu.addEventListener('loaded', function (){
			bbs_inf_huifu.show('pop-in');
			plus.nativeUI.closeWaiting();
		})	
 	})
}

function info(){
	//浏览量与收藏状态 
 	plus.nativeUI.showWaiting('加载中...'); 
 	$.ajax({  
 		
		type:"post", 
		 
		url:apiRoot + '/Home/Article/collection_status/',
		
		data:{
			uid : uid , 
			aid : id ,
			tid : tid
		},
		
		dataType : 'json', 
		 
		success : function(data){ 
			plus.nativeUI.closeWaiting();  
			if(data.status)$('.collection_status').css('color' , 'darkgoldenrod');
			if(data.message != ''){
				$('.total_number').text(data.message.length); 
				var html = '';
				$.each(data.message , function(k ,v){
					html += '<div class="inf-pl overflow-h position-re table-view">';
					html += '<i class="mui-pull-left"><img src="'+getImgUrl(v.avatar)+'"/></i>';
					html += '<div class="mui-pull-left font-14"><li class="font-14"><span>'+v.nickname+'</span></li>';
					html += '<li><span class="color-c font-11">'+v.addtime+'</span><span>';
					if(v.is_adopt == 1){
						html += '<span style="color: #5DB200;margin: 0 1em;"><img src="../images/caina.png" style="width:20px;height:20px;display:inline;vertical-align:top;margin:0;padding:0"><span style="margin: 0 0.5em;">已采纳</span>';
					}else{
						html += '<button type="button" style="background-color: #5DB200;padding: 1px 12px;color: #fff;font-size: 12px;border:none;margin: 0 1em;" onclick="adopt('+v.aid+' , '+v.article_id+' ,'+v.uid+')">采纳</button>';
					}
					html += '</span></span></li><li class="font-14"><span>'+v.body+'</span></li><li class="overflow-h img-div">';
					if(v.pics){
						$.each(v.pics, function(k1 ,v1) { 
							if(v1)html += '<img src="'+getImgUrl(v1)+'"alt="" data-preview-src="" data-preview-group="2"/>';
						});
					}
					html += '</li></div></div>';
				})
				$('.lists').html(html);
				
//				$('.inf-pl').each(function(index){
//					var self = $(this); 
//					if(data.message[index]){
//						self.find('i img').attr('src' , getImgUrl(data.message[index].avatar));
//						self.find('.font-14 span').text(data.message[index].nickname);
//						self.find('.font-11').text(data.message[index].addtime);
//						self.find('.margin-top-5').text(data.message[index].body);
//						
//						if(data.message[index].is_adopt == 1){
//							
//						}else{
//							0$(this).find('.adopt').html('<span style="color: #5DB200;margin: 0 1em;"><i class="iconfont" style="vertical-align: middle;">&#xe63e;</i><span style="margin: 0 0.5em;">已采纳</span></span>');
//							var aid =  data.message[index].aid;
//							var articleid =  data.message[index].article_id;
//							var userid =  data.message[index].uid;
////							$(this).find('.adopt').html('<button type="button" data-aid ="'+aid+'" data-articleid ="'+articleid+'" data-userid ="'+userid+'" style="background-color: #5DB200;padding: 1px 12px;color: #fff;font-size: 12px;border:none;margin: 0 1em;" onclick="adopt('+aid+' , '+articleid+' , '+userid+')">采纳</button>');
//						} 
//						
//						if(data.message[index].pics){ 
//							$.each(data.message[index].pics, function(k ,v) { 
//								if(v)self.find('.img-div').append('<img src="'+getImgUrl(v)+'"alt="" data-preview-src="" data-preview-group="2"/>');
//							});
//						}  
//						 
//						
//					}else{
//						$('.lists').children().eq(1).remove();
//					}
//				})
			}
		},
		error : function(e){
			console.log(JSON.stringify(e))
			toast('连接超时,请检查网络');
			plus.nativeUI.closeWaiting();
		}
		 
	});
}

/**
 * 采纳
 * @param {Object} id 消息
 * @param {Object} articleid 来源
 * @param {Object} userid 用户
 */
function adopt(id , articleid , userid){
	//当前登录账户与发帖帐号id对比
	if(uid != post_man){
		toast('您不是该文章发帖人,无权操作');
		return; 
	}
	
	if(uid == userid){
		toast('您不能采纳自己的消息');
		return;
	}
	
//	console.log(apiRoot+'/Home/Article/adopt/id/'+id +'/articleid/' + articleid +'/userid/' + userid +'/number/'+number);return;
	
	plus.nativeUI.showWaiting('操作中...');
	$.ajax({
		type : 'post',
		url : apiRoot+'/Home/Article/adopt/',
		data:{
			id : id ,
			articleid : articleid,
			userid : userid , 
			number : number
		},
		success: function(data){
			plus.nativeUI.closeWaiting();
			if(data == -1){
				toast('您已经采纳过其他人');
			}else{
				toast('已采纳');
				info();
			}
		},
		error : function(e){
			console.log(JSON.stringify(e))
			toast('连接超时,请检查网络');
			plus.nativeUI.closeWaiting();
		}
		
	})
	
}

window.addEventListener('add_reply',function(event){
	info();
//	$('.total_number').text(parseInt($('.total_number').text()) +1); 
//	var html = '<div class="inf-pl overflow-h position-re table-view">';
//		html +=	'<i class="mui-pull-left"><img src="'+getImgUrl(event.detail.avatar)+'"/></i>';
//		html +=	'<div class="mui-pull-left font-14">';
//		html +=	'<li class="font-14"><span>'+event.detail.nickname+'</span></li>';
//		html +=	'<li><span class="color-c font-11">刚刚</span></li>';
//		html +=	'<li class="overflow-h img-div"> ';
//		html +=	'<div class="margin-top-5">'+event.detail.content+'</div>';
//		
//		if(event.detail.pics){
//			$.each(event.detail.pics, function(k ,v) { 
//				if(v)html +='<img src="'+getImgUrl(v)+'"alt="" data-preview-src="" data-preview-group="2"/>';
//			});
//		}
//		html +=	'</li></div></div>';
//		
//		$('.lists').prepend(html);
//		
//		if($('.lists').children().length > 1)$('.lists').children().eq(2).remove();
//	
});